
package com.william.killian.trivia.model;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.william.killian.trivia.util.Util;

@Root (name = "Quiz")
public class Quiz implements Serializable, Comparable <Quiz>
{
    private static final long serialVersionUID = 1796634874453126331L;
    
    private File file;
    
    @Element (name = "Name")
    private String name;

    @ElementList (inline = true)
    private List <Question> question;

    private int currentQuestion;
    
    private int correct;

    private int incorrect;

    public Quiz () {
        question = new ArrayList <Question> ();
        file = new File (Util.getApplicationDirectory (), System.currentTimeMillis () + ".xml");
        currentQuestion = -1;
        correct = 0;
        incorrect = 0;
    }
    
    public void addQuestion ()
    {
        question.add (new Question ());
    }

    public List <Question> getQuestions ()
    {
        if (question == null)
        {
            question = new ArrayList <Question> ();
        }
        return question;
    }

    public String getName ()
    {
        return name;
    }

    public void start ()
    {
        currentQuestion = -1;
    }

    public boolean hasNextQuestion ()
    {
        return (currentQuestion < question.size () - 1);
    }

    public Question getNextQuestion ()
    {
        return question.get (++currentQuestion);
    }

    public void addWrongAnswer ()
    {
        ++incorrect;
    }

    public void addCorrectAnswer ()
    {
        ++correct;
    }

    public int getCurrentQuestionNumber ()
    {
        return (currentQuestion + 1);
    }

    public int getNumberOfCorrectAnswers ()
    {
        return correct;
    }

    public int getNumberOfIncorrectAnswers ()
    {
        return incorrect;
    }

    public int getNumberOfQuestions ()
    {
        return question.size ();
    }

    public Question getCurrentQuestion ()
    {
        return question.get (currentQuestion);
    }
    
    public int getQuizPercentage()
    {
        return (100 * correct / question.size ());
    }
    
    public File getHighScoreFile ()
    {
        return new File (Util.getHighScoresDirectory (), getHighScoreFileName ());
    }
    
    private String getHighScoreFileName ()
    {
        return (getName ().hashCode () + ".xml");
    }
    
    public void setName (String name)
    {
        this.name = name;
    }
    
    public static Quiz Load (File f)
    {
        try
        {
            Serializer serializer = new Persister ();
            Quiz q = serializer.read (Quiz.class, f);
            q.file = f;
            return q;
        }
        catch (Exception e)
        {
            return null;
        }
    }
    
    public static Quiz Load (String url)
    {
        try
        {
            URL u = new URL (url);
            URLConnection conn = u.openConnection ();
            InputStream is = new BufferedInputStream (conn.getInputStream ());
            File f = new File (Util.getApplicationDirectory (), System.currentTimeMillis () + ".xml");
            OutputStream out = new BufferedOutputStream (new FileOutputStream (f));
            byte[] buf = new byte [1024];
            int len;
            while ((len = is.read (buf)) > 0)
            {
                out.write (buf, 0, len);
            }
            out.close ();
            is.close ();
            Serializer serializer = new Persister ();
            Quiz q = serializer.read (Quiz.class, f);
            q.file = f;
            return q;
        }
        catch (Exception e)
        {
            return null;
        }
    }
    
    public void save ()
    {
        Serializer serializer = new Persister ();
        try
        {
            serializer.write (this, getFile ());
        }
        catch (Exception e)
        {
            
        }
    }
    
    public String toString ()
    {
        return getName ();
    }

    public int compareTo (Quiz another)
    {
        return this.getName ().compareTo (another.getName ());
    }
    
    public File getFile ()
    {
        return file;
    }

    public void deleteQuestion (int pos)
    {
        question.remove (pos - 1);
    }
    
}
